

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

public class ServerConnection {
	
	public static Socket connectionSocket;
	public static ServerSocket serverSocket; 
	
	/**
	 * Open Receiver connection
	 * @param port
	 * @throws Exception
	 */
	public static boolean openListeningConnection(int port){
		boolean ret = true;
		
		try {
			serverSocket = new ServerSocket(port);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			ret = false;
		}
		
		return ret;
	}
	
	public static boolean openConnection(String ipAddress,int port){
		boolean ret = true;
		
		try {
			connectionSocket = new Socket(ipAddress, port);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			ret = false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			ret = false;
		}
		
		return ret;
	}
	
	/**
	 * 
	 * @param port
	 * @return
	 * @throws Exception
	 */
	public static String receive(){
      String message = null;
	  try
	  {
		  //System.out.println("ServerConnection.receive():serverSocket ip"+serverSocket.getInetAddress()+",port:"+serverSocket.getLocalPort());
          connectionSocket = serverSocket.accept();
          BufferedReader receiveObject = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));		          
          message = receiveObject.readLine();
          //if (message == null)
        	  //System.out.println("ServerConnection.receive():serverSocket ip"+serverSocket.get+",port:"+serverSocket.getLocalPort());
      }
	  catch (IOException e) {
		  System.out.println("Connection error (Client crash?)");
	  }
	  return message;
	}
	
	//todo : change Server to Maze
	public static Maze receiveGameState(){
		Maze game = null;
	      
		  try
		  {
			  connectionSocket = serverSocket.accept();
			  InputStream is = connectionSocket.getInputStream();  
			  ObjectInputStream ois = new ObjectInputStream(is);  
			  //System.out.println("received obj = "+ois.readObject());
			  game = (Maze)ois.readObject();
	      }
		  catch (IOException e) {
			  e.printStackTrace();
		  } catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  
		  return game;
	}
	
	public static Maze receiveGame(){
		Maze game = null;
	      
	      if (connectionSocket == null)
	    	  return null;
	      
		  try
		  {
			  InputStream is = connectionSocket.getInputStream();  
			  ObjectInputStream ois = new ObjectInputStream(is);  
			  game = (Maze)ois.readObject();
	      }
		  catch (IOException e) {
			  e.printStackTrace();
			  System.out.println("Connection error (Client crash?)");
		  } catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  
		  return game;
	}
	
	/*
	public static ArrayList<Player> receivePlayer(){
		ArrayList<Player> pList = null;
	      
	      if (connectionSocket == null)
	    	  return null;
	      
		  try
		  {
			  InputStream is = connectionSocket.getInputStream();  
			  ObjectInputStream ois = new ObjectInputStream(is);  
			  pList = (ArrayList<Player>)ois.readObject();
	      }
		  catch (IOException e) {
			  e.printStackTrace();
			  System.out.println("Connection error (Client crash?)");
		  } catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  
		  return pList;
	}
	*/
	public static void send(String message){
		PrintWriter sendObject = null;
		
		try {
			sendObject = new PrintWriter(connectionSocket.getOutputStream(), true);
			sendObject.println(message);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void send(String message, Socket soc){
		PrintWriter sendObject = null;
		
		try {
			sendObject = new PrintWriter(soc.getOutputStream(), true);
			sendObject.println(message);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//to do : change server to maze
	public static void sendGameState(Maze game, Socket soc){
		OutputStream os;
		try {
			os = soc.getOutputStream();
			ObjectOutputStream sendObject = new ObjectOutputStream(os);
			sendObject.writeObject(game);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void closeConnection(){
		try {
			connectionSocket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void closeListeningConnection(){
		try {
			serverSocket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
